package net.lab0.wallstreet;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.lab0.tools.FileUtils;

import com.google.common.collect.Lists;

/**
 * Reads the input data from files
 * 
 * @author 116
 * 
 */
public class DataSource
{
    private String        inputFile;
    private boolean       read;
    private int           transfer;
    private List<Integer> payments = new ArrayList<Integer>();
    
    /**
     * 
     * @param inputFile
     *            The file to read the data from. First number: the bank transfer, next: the due payments
     */
    public DataSource(String inputFile)
    {
        super();
        this.inputFile = inputFile;
    }
    
    /**
     * 
     * @return the transfer
     */
    public int getBankTransfer()
    {
        return transfer;
    }
    
    /**
     * 
     * @return a copy of the payments list
     */
    public List<Integer> getDuePayments()
    {
        return Lists.newArrayList(payments);
    }
    
    /**
     * reads the data from the input file and stores it in this class' fields
     * 
     * @throws IOException
     */
    public void readData()
    throws IOException
    {
        if (read)
        {
            return;
        }
        List<String> data = FileUtils.readFileAsText(new File(inputFile));
        // here we will do the conversion to integers to avoid precision loss
        
        String transferString = data.get(0);
        transferString = transferString.replace(".", "");
        transfer = Integer.parseInt(transferString);
        data = data.subList(1, data.size());
        
        for (String s : data)
        {
            s = s.replace(".", "");
            payments.add(Integer.parseInt(s));
        }
        
        read = true;
    }
}
